package org.rxc.org.service.impl;

import org.rxc.base.service.impl.BaseServiceImpl;
import org.rxc.org.domain.Department;
import org.rxc.org.mapper.DepartmentMapper;
import org.rxc.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        department.setCreate_time(new Date());
        if (department.getParent() == null){
            department.setPath("/"+department.getId());
        }else {
            Department parent = departmentMapper.queryById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        if (department.getParent() == null){
            department.setPath("/"+department.getId());
        }else {
            Department parent = departmentMapper.queryById(department.getParent().getId());
            department.setPath(parent.getPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }
//
//    @Override
//    @Transactional
//    public void delete(Long id) {
//        departmentMapper.delete(id);
//    }
//
//    @Override
//    public Department queryById(Long id) {
//        return departmentMapper.queryById(id);
//    }
//
//    @Override
//    public List<Department> queryAll() {
//        return departmentMapper.queryAll();
//    }
//
//    @Override
//    public PageList<Department> queryPage(BaseQuery baseQuery) {
//        Long total = departmentMapper.count(baseQuery);
//        List<Department> list = departmentMapper.queryPage(baseQuery);
//        return new PageList<Department>(total,list);
//    }

    @Override
    public List<Department> queryDepTree() {
        return departmentMapper.queryDepTree();
    }

}
